package com.javatip.project.deep_learning.app01Dataset.service;

import java.util.List;

import com.javatip.common.utils.file.FileUtils;
import com.javatip.framework.web.domain.AjaxResult;
import com.javatip.project.deep_learning.app01Dataset.controller.App01DatasetController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.javatip.project.deep_learning.app01Dataset.mapper.App01DatasetMapper;
import com.javatip.project.deep_learning.app01Dataset.domain.App01Dataset;
import com.javatip.common.utils.text.Convert;

/**
 * 数据集 服务层实现
 *
 * @author 袁伟
 * @date 2024-06-18
 */
@Service
public class App01DatasetServiceImpl implements IApp01DatasetService {
    @Autowired
    private App01DatasetMapper app01DatasetMapper;



    /**
     * 查询数据集信息
     *
     * @param id 数据集ID
     * @return 数据集信息
     */
    @Override
    public App01Dataset selectApp01DatasetById(Integer id) {
        return app01DatasetMapper.selectApp01DatasetById(id);
    }

    /**
     * 查询数据集列表
     *
     * @param app01Dataset 数据集信息
     * @return 数据集集合
     */
    @Override
    public List<App01Dataset> selectApp01DatasetList(App01Dataset app01Dataset) {
        return app01DatasetMapper.selectApp01DatasetList(app01Dataset);
    }

    /**
     * 新增数据集
     *
     * @param app01Dataset 数据集信息
     * @return 结果
     */
    @Override
    public int insertApp01Dataset(App01Dataset app01Dataset) {
        return app01DatasetMapper.insertApp01Dataset(app01Dataset);
    }

    /**
     * 修改数据集
     *
     * @param app01Dataset 数据集信息
     * @return 结果
     */
    @Override
    public int updateApp01Dataset(App01Dataset app01Dataset) {
        return app01DatasetMapper.updateApp01Dataset(app01Dataset);
    }

    /**
     * 删除数据集对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteApp01DatasetByIds(String ids) {
        try {
            App01Dataset app01Dataset = app01DatasetMapper.selectApp01DatasetById(Integer.parseInt(ids));
            int result = app01DatasetMapper.deleteApp01DatasetByIds(Convert.toStrArray(ids));
            if (result > 0) {
//                App01DatasetController controller = new App01DatasetController();
//                controller.delete(Integer.parseInt(ids));
                deleteApp01DatasetWithDB(ids, app01Dataset.getLocation());
                return AjaxResult.success("删除成功");
            } else {
                return AjaxResult.error("删除失败，没有找到对应的数据");
            }
        } catch (DataIntegrityViolationException e) {
            // 异常处理逻辑，返回 deleteError 的消息
            return AjaxResult.deleteError();  // 确保返回 deleteError 的结果
        }
    }


    @Override
    public App01Dataset selectApp01DatasetByNameAndUserId(String name, int userId) {
        return app01DatasetMapper.selectApp01DatasetByNameAndUserId(name, userId);
    }

    @Override
    public int deleteApp01DatasetWithDB(String ids, String location) {
//		int result = app01DatasetMapper.deleteApp01DatasetByIds(Convert.toStrArray(ids));
        System.out.println("删除数据集图片地址："+location);
//        App01Dataset result = app01DatasetMapper.selectApp01DatasetById(Integer.parseInt(ids));
//		System.out.println("result: " + result);
            FileUtils.deleteFolderFile(location); // 删除文件夹及其内容
        return 1;
    }

    @Override
    public boolean isPredictNameExists(String name, int userId) {
        Integer count = app01DatasetMapper.isPredictNameExists(name,userId);
        return count != null && count > 0;
    }

}
